/*
 * Copyright (c) Jack魏 2023 - 2024, All Rights Reserved.
 */

package cn.jackwei.yiyi.controller.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.jackwei.yiyi.pojo.bean.SysUser;
import cn.jackwei.yiyi.pojo.constant.CommonConsts;
import cn.jackwei.yiyi.pojo.enums.ResultMsgEnum;
import cn.jackwei.yiyi.pojo.query.SysUserQuery;
import cn.jackwei.yiyi.pojo.vo.SysUserUpdateVO;
import cn.jackwei.yiyi.service.sys.SysUserService;
import cn.jackwei.yiyi.util.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台用户管理
 *
 * @author Jack魏
 * @since 2023/10/20
 */
@Slf4j
@Validated
@SaCheckLogin
@RestController
@RequestMapping("/sys")
@Tag(name = "系统用户登录接口", description = "系统用户登录接口")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 更新当前用户信息
     *
     * @return 更新当前用户信息
     */
    @Operation(summary = "更新当前用户信息", description = "更新当前用户信息")
    @PostMapping("/updateUser")
    public Result<Object> updateUser(@RequestBody @Validated SysUserUpdateVO vo) {
        return sysUserService.updateUser(vo);
    }

    /**
     * 根据用户id更新用户信息
     *
     * @return 根据用户id更新用户信息
     */
    @Operation(summary = "根据用户id更新用户信息", description = "根据用户id更新用户信息")
    @PostMapping("/updateUserById")
    public Result<Object> updateUserById(@RequestBody SysUser user) {
        boolean isUpdate = false;
        if (CommonConsts.DEL.equals(user.getIsDel())) {
            isUpdate = sysUserService.removeById(user);
        } else {
            isUpdate = sysUserService.updateById(user);
        }
        if (!isUpdate) {
            return Result.fail(ResultMsgEnum.ERROR_OPERATE.getMsg());
        }
        return Result.success(ResultMsgEnum.SUCCESS.getMsg());
    }

    /**
     * 根据条件查询用户信息
     *
     * @return 根据条件查询用户信息
     */
    @Operation(summary = "根据条件查询用户信息", description = "根据条件查询用户信息")
    @GetMapping("/pageUser")
    public Result<Page> pageUser(SysUserQuery query) {
        return Result.success(sysUserService.pageUser(query));
    }
}
